#include "bgridwidget.h"
#include <QPainter>

BGridWidget::BGridWidget(QWidget *parent) : QWidget(parent) {
    m_pen.setColor(Qt::lightGray);
    m_pen.setWidth(1);
}

void BGridWidget::setGridColor(const QColor &color) {
    m_pen.setColor(color);
    update();
}

void BGridWidget::setGridSpacing(int spacing) {
    m_spacing = spacing;
    update();
}

void BGridWidget::paintEvent(QPaintEvent *) {
    QPainter painter(this);
    painter.setPen(m_pen);

    // 绘制水平线
    for(int y = 0; y < height(); y += m_spacing) {
        painter.drawLine(0, y, width(), y);
    }
    // 绘制垂直线
    for(int x = 0; x < width(); x += m_spacing) {
        painter.drawLine(x, 0, x, height());
    }
}
